'use strict';

const conn = require('../../../libs/Conn');
const moment = require('moment');

module.exports = async function (params) {
    let statusSql = ``;
    if(params.progress_type === '24'){
        let obj =　{
            remarks:params.remarks,
            completion_date:params.completion_date,
            user_id:params.user_id,
            review_pass_date:params.review_pass_date,
            inspection_date:params.inspection_date,
        };
        let rec = await conn(`SELECT * FROM engineering_progress_completion WHERE engineering_id = '${params.engineering_id}'`);
        let updateProgressStatusSql = ``;
        let insertUserSql = ``;
        if(params.user_ids){
            let user_ids=params.user_ids;
            user_ids=user_ids.split(',');
            let deleteSql = `DELETE FROM progress_information_user_middle WHERE information_id = ?`;
            await conn(deleteSql,[rec[0].information_id]);
            for (let user_id in user_ids){
                insertUserSql += `INSERT INTO progress_information_user_middle (information_id, user_id) VALUES ('${rec[0].information_id}', '${user_ids[user_id]}');`;
            }
        }
        if(rec[0].remarks === null && rec[0].completion_date === null && rec[0].user_id === null&& rec[0].review_pass_date === null&& rec[0].inspection_date === null){
            updateProgressStatusSql = `UPDATE engineering_file SET status = 3 WHERE engineering_id = '${params.engineering_id}';UPDATE engineering_progress_file SET completion_acceptance = 2 WHERE engineering_id = '${params.engineering_id}';`
        }
        statusSql = `UPDATE engineering_progress_completion SET ? WHERE engineering_id = ?;`+updateProgressStatusSql + insertUserSql;
        await conn(statusSql,[obj,params.engineering_id]);
        return {success:true};
    }else if(params.progress_type === '25'){
        let obj =　{
            remarks:params.remarks,
            filing_date:params.filing_date,
            user_id:params.user_id,
            inspection_date:params.inspection_date,
        };
        let rec = await conn(`SELECT * FROM engineering_progress_filing WHERE engineering_id = '${params.engineering_id}'`);
        let updateProgressStatusSql = ``;
        if(moment(rec[0].create_time).format() === moment(rec[0].update_time).format()){
            updateProgressStatusSql = `UPDATE engineering_file SET status = 4 WHERE engineering_id = '${params.engineering_id}';UPDATE engineering_progress_file SET completion_filing = 2 WHERE engineering_id = '${params.engineering_id}';`
        }
        statusSql = `UPDATE engineering_progress_filing SET ? WHERE engineering_id = ?;`+updateProgressStatusSql;
        await conn(statusSql,[obj,params.engineering_id]);
        return {success:true};
    }
};